package thread.join;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 多线程测试，主要是tools包下面的工具类都没用到过【join】
 *
 * @author wei.yang on 2018/8/7
 */
public class MultiThreadTest {

    public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(new Worker());
        t1.start();
        t1.join(10000);
        Thread t2=new Thread(new Worker());
        t2.start();
        ExecutorService service=Executors.newFixedThreadPool(5);
        for (int i=0;i<20;i++){
            final int finalI = i;
            service.submit(() -> System.out.println(finalI));
            // 并行变串行，其实就是让主线程去等待当前子线程执行完或执行一段时间
            Thread.currentThread().join(5);
        }
    }

    /**
     * worker
     */
    static class Worker implements Runnable {

        /**
         * worker content
         */
        @Override
        public void run() {
            System.out.println("thread name:\t" + Thread.currentThread().getName());
        }
    }
}
